| No. | Answer | Remark | |
|---|---|---|---|
| 1 | f | Prints: fffffffe,ffffffff,7fffffff | The left shift operator << shifts each bit of the left operand to the left a distance specified by the right operand. The right operand specifies the shift distance. For each bit position of the shift distance a bit that is set to zero is shifted into the least significant bit. The least significant bit is the right most bit. The right shift operator >> shifts each bit of the left operand to the right a distance specified by the right operand. The right operand specifies the shift distance. For each bit position of the shift distance a copy of the sign bit is shifted to the right as each bit of the left operand is shifted to the right. As a result, the sign of the left operand remains unchanged by the shift operation. The sign bit is the left most bit of the left operand. The unsigned right shift operator >>> shifts each bit of the left operand to the right a distance specified by the right operand. The right operand specifies the shift distance. For each bit position of the shift distance a bit that is equal to zero is shifted into the sign bit as each bit of the left operand is shifted to the right. Therefore, the sign of the result is always positive. |
| 2 | f | Prints: fffffffe,ffffffff,fffffffe | |
| 3 | d | Prints: 0.0,-0.0,-0.0 | If the result is not NaN, then the sign of the result is equal to the sign of the dividend. |
| 4 | j | Compiler error | The expression used to set the value of variable b3, "r1 instanceof Blue" causes a compiler error because the type of variable r1, Red, can not be converted to type Blue. The expressions used to set variables b1 and b2 are legal. The first of the two would produce a value of true and the second would produce a value of false. |
| 5 | d | Prints: true,true | The sign of a number is changed by inverting all of the bits and by adding one. |
| 6 | f | Prints: 3,0,3 | Java evaluates expressions from left to right while respecting operator precedence. The order of operator precedence starting with the lowest is as follows: |, ^, &. Although complete memorization of the operator precedence chart is not necessary, the level required for this question is required. |
| 7 | a | Prints: NaN,NaN | If either operand is NaN then the result is NaN. |
| 8 | a | Prints: 1 | Java evaluates expressions from left to right while respecting operator precedence. The "and" operator has higher precedence than the "exclusive or" operator. The "or" operator has lower precedence. Although complete memorization of the operator precedence chart is not necessary, the level required for this question is required. (1 | ((2 ^ (15 & 7)) ^ 13) | 2), (1 | ((2 ^ (7)) ^ 13) | 2), (1 | ((5) ^ 13) | 2), ((1 | 8 | 2)==11), (11%5=1) |
| 9 | b | Prints: false, false, true | Float values are converted to an integer before being converted to a short. The most negative value of type int is represented as 0x80000000 and becomes 0x0000 when converted to a short. The most positive value of type int is represented as 0x7FFFFFFF and becomes 0xFFFF when converted to a short. NaN is converted to zero. |
| 10 | b | Prints: false true false | The equality operator has higher precedence than the logical operators; therefore, the left side, (a | b == c & d), is evaluated as (a | (b == c) & d). Substituting the actual boolean values for the variable names produces (false | (false == true) & false) and evaluates to false. |
| 11 | f | Compiler error. | Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, no method is clearly more specific than all of the others. |
| 12 | f | Prints: true,false,true | The left operand of the "instanceof" operator must be a reference to an instance of an Object or subclass of Object. The right operand of the "instanceof" operator must be an Object type. If the left operand is a reference to an instance of the type specified by the right operand or if the left operand is a reference to an instance of a subclass of the type specified by the right operand, then instanceof returns true. |
| 13 | e | Compiler error at line 7. | Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa. |
| 14 | e | Prints: 10 | If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to an int. If the promoted type of the left hand operand is of type int, then the shift operator only uses the least significant five bits of the right hand operand and the actual shift distance is always less than 32 bits. Therefore, if the right hand operand is 33 then the shift distance is one bit because the five least significant bits are 00001. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are used. |
| 15 | b | Prints: 1, 2, 3, 4, 11, | Java evaluates expressions from left to right. The first operator encountered is the subtraction operator. First, the left operand is evaluated as one and then the right operand is evaluated as two and then the subtraction is performed resulting in a value of negative one. The next operand encountered is the addition operator. The value of the left operand is the result of the previous operation, -1. The value of the right operand will be the result of the expression m(3) * m(4) which is evaluated as 12. The addition operator adds the left hand operand, -1, to the right hand operand, 12, to prodduce a value of 11. |
| 16 | a | Prints: -4 | If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int. When a variable of type int with a value of 127 is shifted to the left two bits, the result is 508. However, the compound assignment operator includes an implicit cast to the type of the left hand operand. E1 op= E2 is equivalent to E1=(T)((E1) op (E2)), where T is the type of the left hand operand. Therefore, when 508 is cast to an eight bit byte, the result is negative four. |
| 17 | c | Prints: 1, 2, 3, 4, 9, | Java evaluates expressions from left to right. The first operator encountered is the addition operator. First, the left operand is evaluated as 1. The value of the right operand will be the result of the expression m(2) % m(3) * m(4). When Java encounters the remainder operator, "%", it first evaluates the left hand operand, 2, and then the right, 3. The result is 2. The next operator is the multiplication operator. The left operand is the result of the previous operation, 2. The right operand is 4. The result is 8. Java then adds the left operand, 1, of the addition operator to the value of the right operand, 8. The result is 9. |